
class ListNode {
     int val;
     ListNode next;
     ListNode(int x) {
         val = x;
         next = null;
    }
}

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null){
            return null;
        }

        ListNode curA =headA;
        ListNode curB =headB;
        int lenA = 1;
        int lenB = 1;
        while(curA != null){
            lenA++;
            curA = curA.next;
        }
        while(curB != null){
            lenB++;
            curB = curB.next;
        }
        curA =  headA;
        curB =  headB;
        int len = lenA - lenB;
        if(len < 0){
            len = lenB - lenA;
            while(len != 0){
                curB = curB.next;
                len--;
            }
        }else{
            while(len != 0){
                curA = curA.next;
                len--;
            }
        }
        while(curA != curB){
            curA = curA.next;
            curB = curB.next;
        }
        if(curA == curB){
            return curA;
        }else{
            return null;
        }
    }
}